#!/usr/bin/env zsh
# Description:
#   Update installed packages in parallel

local     is_force=false
local     repo arg
local -A  repo_pids proc_states hook_build hook_finished hook_pids status_codes
local -A  tags
local -F  SECONDS=0
local -aU repos
local -i  status_code=0

while (( $# > 0 ))
do
    arg="$1"
    case "$arg" in
        --select)
            zstyle ':zplug:core:update' 'select' yes
            ;;
        --force)
            is_force=true
            ;;
        -*|--*)
            __zplug::core::options::unknown "$arg"
            return $status
            ;;
        "")
            # Invalid
            return 1
            ;;
        */*)
            repos+=( "${arg:gs:@::}" )
            ;;
        *)
            return 1
            ;;
    esac
    shift
done

if ! __zplug::job::parallel::init "$repos[@]"; then
    # Since the initialization has failed, this command is terminated.
    # The error message etc. should be done within that function.
    return 1
fi
repos=( "$reply[@]" )

for repo in "$repos[@]"
do
    __zplug::core::tags::parse "$repo"
    tags=( "${reply[@]}" )

    # Run the updating in subprocess
    {
        if [[ -d $tags[dir] ]]; then
            if ! $is_force && (( $_zplug_boolean_true[(I)$tags[frozen]] )); then
                status_code=$_zplug_status[skip_frozen]
            else
                if __zplug::core::sources::is_handler_defined "update" "$tags[from]"; then
                    __zplug::core::sources::use_handler \
                        "update" \
                        "$tags[from]" \
                        "$repo"
                    status_code=$status
                else
                    status_code=$_zplug_status[skip_local]
                fi
            fi
        else
            status_code=$_zplug_status[repo_not_found]
        fi

        # Manage the status codes in a file
        # to lock the file in order to write asynchronously
        __zplug::job::handle::flock \
            "$_zplug_log[update]" \
            "repo:$repo\tstatus:$status_code"
    } &

    repo_pids[$repo]=$(builtin printf $!) # for zsh 5.3
    hook_build[$repo]="$tags[hook-build]"
    hook_finished[$repo]=false
    proc_states[$repo]="created"
    status_codes[$repo]=""

    __zplug::job::handle::wait
done

__zplug::job::handle::elapsed_time $SECONDS
__zplug::job::parallel::deinit
